<template>
  <div class="at-box">
    <div class="at-top">
      <span>私信 </span>
    </div>

    <div class="at-box" v-for="msg in msgs" :key="msg.user.id">
      <div class="private-img">
        <img :src="msg.fromUser.avatarUrl" alt="" />
        <span v-if="msg.newMsgCount >= 1">{{ msg.newMsgCount }}</span>
      </div>

      <div class="private-span">
        <div class="private-add">
          <span class="span1">{{ msg.fromUser.nickname }}</span>
          <span>{{ moment(msg.lastMsgTime).format("MM月DD日 HH:mm") }}</span>
        </div>
        <span class="private-msg">{{ JSON.parse(msg.lastMsg).msg }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import moment from "moment";
export default {
  data() {
    return {
      // 偏移量
      offset: 0,
      msgs: [],
      moment,
    };
  },
  mounted() {
    this.getmsgprivate();
  },
  methods: {
    async getmsgprivate() {
      /* limit : 返回数量 , 默认为 30
      offset  : 偏移数量，用于分页 , 如 :( 页数 -1)*30, 
      其中 30 为 limit 的值 , 默认为 0
      接口地址 : /msg/private
      调用例子 : /msg/private?limit=3 */
      const data = {
        cookie: this.cookie,
        limit: 10,
        offset: this.offset,
      };
      const result = await this.$API.reqmsgprivate(data);
      console.log(result);
      if (result.code == 200) {
        this.msgs = result.msgs || [];
        this.changenums();
      } else {
        this.$msg.error("获取私信失败");
      }
    },
    // 给父级传参
    changenums() {
      let sum = 0;
      this.msgs.forEach((item) => {
        sum = sum + item.newMsgCount;
      });
      // this.$emit('changenum',sum)
      localStorage.setItem("prtivatesum", sum);
      this.$store.commit("CHANGPRTIVSUM", sum);
      // console.log( localStorage.getItem("prtivatesum"))
    },
  },
  computed: {
    ...mapState({
      //cookie
      cookie: (state) => {
        return state.User.loginmessage.cookie;
      },
      loginmessage: (state) => {
        return state.User.loginmessage;
      },
    }),
  },
  watch: {
    // cookie(val) {
    //   console.log(val);
    //   this.getmsgprivate();
    // },
     loginmessage(val,o) {
      console.log(val,o);
      this.getmsgprivate();
    },
  },
};
</script>

<style lang="less" scoped>
.at-box {
  width: 715px;
  margin: 0px auto;
  .at-top {
    border-bottom: 3px solid red;
    margin-top: 30px;
    span {
      display: block;
      margin-bottom: 5px;
    }
  }
  .at-box {
    height: 83px;
    display: flex;
    align-items: center;
    .private-img {
      position: relative;
      img {
        width: 50px;
        height: 50px;
      }
      span {
        position: absolute;
        left: -7px;
        top: -10px;
        display: block;
        font-size: 12px;
        width: 13px;
        line-height: 13px;
        height: 12px;
        background-color: red;
        border-radius: 14px;
        text-align: center;
        border: solid 3px #fff;
      }
    }
    .private-span {
      padding: 10px;
      font-size: 12px;
      color: #999;
      width: 100%;

      .private-add {
        display: flex;
        justify-content: space-between;
        margin-top: -14x;
        .span1 {
          font-size: 16px;
          display: block;
          color: #000;
          font-weight: bold;
          margin-bottom: 8px;
        }
      }
      .private-msg {
        width: 80%;
        // display: block;
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
      }
    }
  }
}
</style>
